package com.mdlib.helper.module.send.order.activitys;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.ljlib.core.base.BaseCommonActivity;
import com.ljlib.core.base.BaseFragment;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.OrderIdInfo;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.fragments.sect.money.OrdersFragment;
import com.mdlib.helper.utils.ToastUtil;

import static com.mdlib.helper.api.MDPresenter.addSubscribe;

/**
 * 作者：HQY on 16/12/15 10:22
 * 邮箱：hqy_xz@126.com
 */

public class PayOrderActivity extends BaseCommonActivity {

    private Dialog mOutOrder;
    private OrderIdInfo orderIdInfo;

    public static Intent newIntent(Context context, OrderIdInfo idInfo) {
        Intent intent = new Intent(context, PayOrderActivity.class);
        intent.putExtra(UIHelper.ORDERIDINFO, idInfo);
        return intent;
    }

    @Override
    protected BaseFragment getFirstFragment() {
        return OrdersFragment.newInstance((OrderIdInfo) getIntent().getExtras().getSerializable(UIHelper.ORDERIDINFO));
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            mOutOrder = new Dialog(PayOrderActivity.this, R.style.dialog);
            mOutOrder.setContentView(R.layout.dialog_out_order);
            TextView contiune = (TextView) mOutOrder.findViewById(R.id.tv_dialog_continue);
            TextView out = (TextView) mOutOrder.findViewById(R.id.tv_dialog_out);
            contiune.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOutOrder.dismiss();
                }
            });
            out.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    closeOrder();
                    mOutOrder.dismiss();
                }
            });
            mOutOrder.show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void closeOrder() {
        orderIdInfo = (OrderIdInfo) getIntent().getExtras().getSerializable(UIHelper.ORDERIDINFO);
        addSubscribe(DataManager.getInstance().getUserApi()
                .closeOrder(orderIdInfo.getBusId())
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        ToastUtil.showToast(baseResult.msg);
                        finish();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }

}
